#include "UpdateWorker.h"
#include <unistd.h>
#include <fcntl.h>
#include "BootSetting.h"
#include <QFile>
#include "MiscData.h"
#include "McuUpdateWorker.h"
#include "Mcu.h"

UpdateWorker *UpdateWorker::getInstance()
{
    static UpdateWorker singleton{};
    return &singleton;
}

void UpdateWorker::startWorkSlot()
{
    start(LowPriority);
}

void UpdateWorker::mcuUpdateCompleteSlot()
{
    emit syncInfoSignal(Info_Type, tr("升级完成"));
    emit completeWorkSignal();
}

UpdateWorker::UpdateWorker()
{
    QObject::connect(McuUpdateWorker::getInstance(), &McuUpdateWorker::mcuUpdateCompleteSignal,
                     this, &UpdateWorker::mcuUpdateCompleteSlot);

    QObject::connect(this, &UpdateWorker::startEnterMcuUpdateSignal,
                     McuUpdateWorker::getInstance(), &McuUpdateWorker::startEnterMcuUpdateSlot);

    QObject::connect(Mcu::getInstance(), &Mcu::mainframeMcuUpdateRequestSignal,
                     McuUpdateWorker::getInstance(), &McuUpdateWorker::mcuUpdateRequestSlot);
}

void UpdateWorker::work()
{
#ifdef arm
    QStringList cmd;
    QString command;
    int status = -1;

    /* 升级fpga */
    if (RuntimeData::MiscData::getInstance()->g_miscDataInfo.isUpdateFpga > 0) {
        emit syncInfoSignal(Info_Type, tr("开始升级fpga"));

        if(!QFile::exists("/dev/mtd0")) {
            emit syncInfoSignal(Error_Type, tr("fpga升级失败，找不到flash"));
            return;
        }

        cmd.clear();
        cmd << QString("flashcp -v /run/media/sda*/update/fpga/*.bin /dev/mtd0");
        command = cmd.join(";");
        qDebug()<<"update fpga cmd:"<<command;

        status = system(command.toUtf8().data());
        if((status < 0) || (!WIFEXITED(status)) || (0 != WEXITSTATUS(status))) {
            qDebug()<<"update arm failed";
            emit syncInfoSignal(Error_Type, tr("fpga升级失败，指令错误"));
            return;
        }
        sync();
        sleep(1);
    }

    /* 升级logo: 启动logo和ui logo */
    if (RuntimeData::MiscData::getInstance()->g_miscDataInfo.isUpdateLogo > 0) {
        emit syncInfoSignal(Info_Type, tr("开始升级logo"));

        cmd.clear();
        cmd << QString("mkdir /home/root/logo");
        cmd << QString("cp /run/media/sda*/update/logo/* /home/root/logo/ -rf");
        cmd << QString("cp /run/media/sda*/update/logo/* /usr/bin/ -rf");
        cmd << QString("chmod 755 /usr/bin/psplash");
        cmd << QString("chmod 755 /usr/bin/psplash-write");
        command = cmd.join(";");
        qDebug()<<"update logo cmd:"<<command;

        status = system(command.toUtf8().data());
        if((status < 0) || (!WIFEXITED(status)) || (0 != WEXITSTATUS(status))) {
            qDebug()<<"update logo failed";
            emit syncInfoSignal(Error_Type, tr("logo升级失败，指令错误"));
            return;
        }
        sync();
        sleep(1);
    }

    /* 升级arm: qt应用程序，脚本, 驱动 */
    if (RuntimeData::MiscData::getInstance()->g_miscDataInfo.isUpdateArm > 0) {
        emit syncInfoSignal(Info_Type, tr("开始升级arm"));

        cmd.clear();
        cmd << QString("cp /run/media/sda*/update/arm/* /home/root/ -rf");
        cmd << QString("chmod 755 endoscopeApp");
        command = cmd.join(";");
        qDebug()<<"update arm cmd:"<<command;

        status = system(command.toUtf8().data());
        if((status < 0) || (!WIFEXITED(status)) || (0 != WEXITSTATUS(status))) {
            qDebug()<<"update arm failed";
            emit syncInfoSignal(Error_Type, tr("arm升级失败，指令错误"));
            return;
        }
        sync();
        sleep(1);
    }
    /* 升级system: Linux内核、设备树 */
    if (RuntimeData::MiscData::getInstance()->g_miscDataInfo.isUpdateSystem > 0) {
        emit syncInfoSignal(Info_Type, tr("开始升级system"));

        cmd.clear();
        cmd << QString("cp /run/media/sda*/update/system/* /boot/ -rf");
        command = cmd.join(";");
        qDebug()<<"update system cmd:"<<command;

        status = system(command.toUtf8().data());
        if((status < 0) || (!WIFEXITED(status)) || (0 != WEXITSTATUS(status))) {
            qDebug()<<"update arm failed";
            emit syncInfoSignal(Error_Type, tr("system升级失败，指令错误"));
            return;
        }
        sync();
        sleep(1);
    }

    /* 升级mcu */
    if (RuntimeData::MiscData::getInstance()->g_miscDataInfo.isUpdateMcu > 0) {
        emit syncInfoSignal(Info_Type, tr("开始升级mcu"));
        cmd.clear();
        cmd << QString("cp /run/media/sda*/update/mcu/mcu.bin /home/root/ -rf");
        command = cmd.join(";");
        qDebug()<<"update mcu cmd:"<<command;

        status = system(command.toUtf8().data());
        if((status < 0) || (!WIFEXITED(status)) || (0 != WEXITSTATUS(status))) {
            qDebug()<<"update mcu failed";
            emit syncInfoSignal(Error_Type, tr("mcu升级失败，指令错误"));
            return;
        }
        sync();
        sleep(1);
        emit startEnterMcuUpdateSignal("/home/root/mcu.bin");
    } else {
        emit completeWorkSignal();
    }
#else
    QThread::msleep(5000);/* 测试 */
#endif
}

void UpdateWorker::run()
{
    qDebug()<<"UpdateWorker running";
    work();
}
